﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;

namespace Web
{
    public partial class news : System.Web.UI.Page
    {
        private int Nowpage = 1;
        private const int PAGESIZE = 18;
        private int CategoryID = 0;
        private int pagecount = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "新闻";
            try
            {
                Nowpage = Int32.Parse(Request.QueryString["pid"].ToString());
            }
            catch
            {
                Nowpage = 1;
            }
            try
            {
                CategoryID = Int32.Parse(Request.QueryString["cid"].ToString());
            }
            catch
            {
                CategoryID = 0;
            }
            Session["NCategoryID"] = CategoryID;
            try
            {
                pagecount = NewsInfoLogic.GetPageCount(PAGESIZE, CategoryID);
            }
            catch
            {
                pagecount = 0;
            }

            if (!IsPostBack)
            {
                UpdateParamInfoLogic.UpdateAllNewsAbout();
                NewsCategoryBindData();
                lbl_NowPage.Text = Nowpage.ToString();
                PageBindData(CategoryID,Nowpage);
                PageIndexBindData(pagecount, Nowpage, CategoryID);
                
            }
            try
            {
                Nowpage = Int32.Parse(lbl_NowPage.Text.Trim());
            }
            catch
            { }
            if (Nowpage > 1) {
                string script = "PageNotOne();";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "pager", script, true);
            }
        }

        public void NewsCategoryBindData()
        {
            List<NewsCategoryInfo> newsCategoryList = NewsCategoryInfoLogic.GetAllNewsCategoryInfo(CategoryID);

            if (newsCategoryList == null)
            {
                newsCategoryList = new List<NewsCategoryInfo>();
                newsCategoryList.Add(new NewsCategoryInfo());
            }
            NewsCategoryRepeater.DataSource = newsCategoryList;
            NewsCategoryRepeater.DataBind();
        }

        public void PageBindData(int CategoryID,int nowpage)
        {
            List<NewsInfo> NewsInfoList = new List<NewsInfo>();
            List<NewsInfo> firstList = new List<NewsInfo>();
            List<NewsInfo> secondList = new List<NewsInfo>();
            List<NewsInfo> thirdList = new List<NewsInfo>();
            if (CategoryID == 0)
            {
                NewsInfoList = NewsInfoLogic.GetAllNewsInfo(PAGESIZE, nowpage);
            }
            else
            {
                NewsInfoList = NewsInfoLogic.GetCategoryNewsInfo(CategoryID, PAGESIZE, nowpage);
            }

            for (int i = 0; i < NewsInfoList.Count; i++)
            {
                if (i < 6)
                {
                    firstList.Add(NewsInfoList[i]);
                }
                else if (i >= 6 && i < 12)
                {
                    secondList.Add(NewsInfoList[i]);
                }
                else
                {
                    thirdList.Add(NewsInfoList[i]);
                }
            }

            NewsDataListfirst.DataSource = firstList;
            NewsDataListfirst.DataBind();

            NewsDataListSecond.DataSource = secondList;
            NewsDataListSecond.DataBind();

            NewsDataListThird.DataSource = thirdList;
            NewsDataListThird.DataBind();
        }

        public void PageIndexBindData(int Pagecount, int NowPage, int CategoryID)
        {
            List<PageIndexInfo> list = PageIndexLogic.GetPageIndexInfo(Pagecount, Nowpage, CategoryID);
            if (list == null)
            {
                list = new List<PageIndexInfo>();
                list.Add(new PageIndexInfo());
            }
            PageRepeater.DataSource = list;
            PageRepeater.DataBind();
            if (Pagecount < 2)
            {
                PageRepeater.Visible = false;
            }
            else
            {
                PageRepeater.Visible = true;
            }
        }
    }
}
